---
title: "Appendix"
output:
  html_document:
    df_print: paged
---

```{r}
help(hist) 
help(log) 
```

## Basic R Commands
```{r}
x <- c(2,4,5,7)
x
```

```{r}
x = c(2,4,5,7)  
x
```

Let us consider the thickness measurements on the tabs used to close 5-gallon paint cans 
(n = 135, measured in microns). We enter the data into the object "thick".
```{r}
thick = c(29,36,39,34,34,29,29,28,32,31,34,34,39,38,37,
35,37,33,38,41,30,29,31,38,29,34,31,37,39,36,
30,35,33,40,36,28,28,31,34,30,32,36,38,38,35,
35,30,37,35,31,35,30,35,38,35,38,34,35,35,31,
34,35,33,30,34,40,35,34,33,35,34,35,38,35,30,
35,30,35,29,37,40,31,38,35,31,35,36,30,33,32,
35,34,35,30,36,35,35,31,38,36,32,36,36,32,36,
36,37,32,34,34,29,34,33,37,35,36,36,35,37,37,
36,30,35,33,31,35,30,29,38,35,35,36,30,34,36)
```

### Prints out data 
```{r}
thick
print(thick) 
```

```{r}
thick[12]		                	#is the 12th observation in the list.
thickordered = order(thick)	  #orders the data set from smallest to the largest. 
thickordered
thickordered[12]		         	#is the observation with rank 12
```

### various operations 

```{r}
summary(thick)
#calculates summary statistics (min, first quartile, median, mean, third quartile, max)
```
```{r}
quantile(thick, probs = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9))
#calculates the percentiles of order 0.1, 0.2, …, 0.9
```

These following commands calculate and store in the object to the left of the equality, the mean, median, standard deviation, and variance of the 135 observations.
```{r}
a1 = mean(thick)
a2 = median(thick)
a3 = sd(thick)
a4 = var(thick) 
```

```{r}
a4   #prints out the variance 
```

```{r}
boxplot(thick)  #creates the box and whisker diagram
```

```{r}
hist(thick)
#creates the histogram
#for further options, look at the help command for hist
```

```{r}
#install.packages("BHH2")
library(BHH2)
dotPlot(thick)   #creates the dotplot. 
#Note that the package BHH2 is needed.
#comment the installation code after use, only need to install #packages once however need to load packages 
```

## Entering data and text into an R-session

Let us consider the data weight (in 1000 pounds) and the fuel efficiency (in gallons per 100 miles) of n = 10 cars listed below (see Abraham and Ledolter (2006)). The third column represents the name of the car.

### reading in data

You can enter the data by reading in each variable individually: 
```{r}
ygpm = c(5.5,5.9,6.5,3.3,3.6,4.6,2.9,3.6,3.1,4.9)   		#gallons per 100 miles
xweight = c(3.4,3.8,4.1,2.2,2.6,2.9,2.0,2.7,1.9,3.4)		#weight in 1000 pounds
ygpm			#prints out ygpm
xweight 	#prints out xweight
```


can also read the data from external text files: 
- read in a csv file
```{r}
data = read.csv("C:\\Johannes Ledolter\\2020March01Book\\AppendixWEB\\FuelEfficiency.csv") 
data
```

- read in a Notepad file 
```{r}
hook = read.table("C:\\Johannes Ledolter\\2020March01Book\\AppendixWEB\\hooker.txt", header = T)
```

### more commands for hook dataset 

#### data transformation
```{r}
Temp = hook[,1]    #define the first column of the matrix “hook” as Temp (the boiling temperature of water) 
Temp
AP = hook[,2]      #and the second column as AP (the atmospheric pressure)
AP
LnAP = 100*log(AP) #transforming the variable AP; log(AP) is the natural log of AP
```

#### linear regression 
```{r}
hookfit = lm(Temp ~ LnAP) # fitting a linear regression
summary(hookfit)          # a comprehensive summary of the results of the regression analysis
anova(hookfit)            # the analysis of variance (ANOVA) table 
```


#### plots 
```{r}
plot(LnAP, Temp)          # plots Temp (the y-coordinate) against LnAP (the x-coordinate). 
abline(hookfit)           # a fitted line added to the plot 
qqnorm(hookfit$residuals) # normal probability plot of the residuals
```


### fitting a multiple linear regression on made-up data
```{r}
x1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 
x2 = c(5, 6, 7, 8, 8, 6, 3, 2, 4, 5)
x3 = c(9, 10, 11, 12, 20, 32, 44, 21, 43, 65)
y = c(2, 4, 6, 8, 7 , 8, 4, 6, 11, 12)
mregfit = lm(y ~ x1 + x2 + x3)  # with intercept
summary(mregfit)
```

```{r}
mulregfit = lm(y ~ x1 + x2 + x3 - 1)  # without intercept 
summary(mulregfit)
```

## Useful R commands for textual data analytics 

### string 
```{r}
mytext = "the weather of today is nice"   # string object mytext is created with double quotes
mytext

mytext = 'the weather of today is nice'   # string object mytext is created using simple quotes
mytext
```


```{r}
mytext = "the weather of TODAY is nice"
nchar(mytext)		  # count the number of characters
toupper(mytext)		# change all the characters of the string to upper case
tolower(mytext)		# change all the characters of the string to lower case
```

### a vector of strings
```{r}
myvector = c("the weather of today is nice", "Today is a good day")       
myvector   # creates the vector myvector with two elements; each element contains a string
nchar(myvector)		# returns the number of characters of each string in the vector
length(myvector)	# the number of elements (strings) in the vector
```

```{r}
mytext = paste("the weather", "of today", "is nice")		# paste the three strings into one string and separate strings by a space
mytext
length(mytext)
mytext = paste("the weather", "of today", "is nice", sep = ",")	   # paste the three strings into one and separate strings by “,”
mytext
length(mytext)
```

```{r}
myvector = c("the weather", "of today", "is nice")		# creates a vector containing three strings
myvector
mytext = paste(myvector, collapse = " ")	   # the three strings in the vector are pasted into one and separated by a space
mytext
length(mytext)
```

```{r}
mytext = "the weather of today is nice"
substr = substring(mytext, 3, 8)		# extract a sub-string from the 3rd to 8th character
substr
```

```{r}
myvector = c("The WEATHER", "the weather", "THE WEather")		
grep("EA", myvector)			 # find the strings that contain “EA”
grep("EA", myvector, ignore.case = TRUE)   # since grep is case sensitive, use ignore.case = TRUE.
grep("EA", myvector, value = TRUE)   # use the argument value=TRUE to return the strings
```

```{r}
myvector = c("the weather of today is NICE", "it is nice")
myvecrepl = gsub("nice", "good", myvector)	  # replace “nice” with “good”, the “NICE” is not replaced
myvecrepl
myvecrepl = gsub("nice", "good", ignore.case = TRUE, myvector)	 # both “NICE” and “nice” are replaced	
myvecrepl
```

```{r}
myvector = c("the weather of today is nice", "it is nice")
strsplit(myvector, " ")			# split the strings by a space " "

# strsplit returns a list. If we want a character vector, combine the list 

unlist(strsplit(myvector, " "))
```


## Useful R commands for reading text data from files

### csv
```{r}
data = read.csv("C:\\Johannes Ledolter\\2020March01Book\\Chapter1WEB\\test.csv", header = FALSE, stringsAsFactors = FALSE)
dim(data)
data[,1]
```

```{r}
str(data)		  # shows the class of each column
dim(data)		  # shows the dimension of the data frame
nrow(data)		# number of rows
ncol(data)		# number of columns 
summary(data)	# shows the statistical summary
```

### txt file 
```{r}
data = readLines("C:\\Johannes Ledolter\\2020March01Book\\AppendixWEB\\combine39.txt")
class(data)		  # examine the class of mytext
length(data)		# the number of elements in the vector
data[1:3]
```

two ways of writing the string vector “mytext” into a file
```{r}
writeLines("The weather of today is nice", "C:\\Johannes Ledolter\\2020March01Book\\AppendixWEB\\mytext.txt")
```

```{r}
mytext = "The weather of today is good"
#writeLines(mytext, "C:\\Johannes Ledolter\\2020March01Book\\AppendixWEB\\mytext.txt")
```

### XML
```{r}
library(XML)
root = xmlParse("C:\\Johannes Ledolter\\2020March01Book\\AppendixWEB\\23.xml") 
```

```{r}
year = xpathSApply(root, "//year", xmlValue, recursive = FALSE)		  # year letter written  
year[1:3]
```

```{r}
aut = xpathSApply(root, "//from", xmlValue, recursive = FALSE)		  # author of letter
aut[1:3]
```

```{r}
rec = xpathSApply(root, "//to", xmlValue, recursive = FALSE)			  # recipient of letter
rec[1:3]
```

```{r}
header = xpathSApply(root, "//header", xmlValue, recursive = FALSE)	# header of letter
header[1:3]
```

```{r}
text = xpathSApply(root, "//record/body", xmlValue, recursive = FALSE)	# text of letter
text[1:3]
```
